﻿package com.cilico.single.view;

import android.view.View;

import com.cilico.single.ConfigManager;
import com.cilico.single.Mid;
import com.cilico.single.components.AtomParser;
import com.cilico.single.components.ComponentListener;
import com.cilico.single.components.ImageItem;
import com.cilico.single.components.Label;
import com.cilico.single.components.POI;
import com.cilico.single.components.PoiSelection;
import com.cilico.single.components.Select;
import com.cilico.single.components.TabItem;
import com.cilico.single.components.Table;
import com.cilico.single.components.mButton;
import com.cilico.single.components.mViewFlipper;
import com.cilico.single.utils.Utils;

//页面操作类 处理详细页面事件
public class DetailController implements ComponentListener {
	private Mid mid_;
	private DetailView detailView_;

	/** Creates a new instance of DetailController */
	public DetailController(DetailView detailView) {
		mid_ = BaseView.mid_;
		detailView_ = detailView;
	}

	public boolean componentAction(View component, BaseView bv) {
		BaseView.ttimerIscannel_ = true;
		if (Table.tableTimer_ != null) {
			Table.tableTimer_.cancel();
			Table.tableTimer_ = null;
		}
		if (mViewFlipper.imgsTimer_ != null){
			mViewFlipper.imgsTimer_.cancel();
			mViewFlipper.imgsTimer_ = null;
		}
		
		boolean isAction = false;
		int index = TabItem.currentTab_;
		if (component == detailView_.tabBar_.tab_) {
			try {
				if (bv.tabBar_ != null)
					bv.removeAll();
				mid_.parser_.dispose();
				mid_.parser_ = null;
				mid_.parser_ = new AtomParser(mid_);
				bv.disposeInChangeChannel();
			} catch (Exception e) {
				Utils.printOutToConsole("清空资源时的警告，可以忽略：" + e.getMessage());
			}
			mid_.channelEntity_.changeChannel(detailView_, index, false);
		} else if (detailView_.detailType_ == DetailView.CHANNEL_LIST) {
			componentActionChannelListView(component);
		} else if (detailView_.detailType_ == DetailView.POI_LIST) {
			componentActionPOIListView(component);
		} else if (detailView_.detailType_ == DetailView.POI_DETAIL
				|| detailView_.detailType_ == DetailView.POI_ONLY_ONE) {
			isAction = componentActionPOIDetailView(component);
		}
		return isAction;
	}

	private boolean componentActionPOIDetailView(View component) {
		boolean isAction = true;
		if (component instanceof mButton) {
			return GeneralController.disposeButtonEvent(component, detailView_);
		} else if (component == detailView_.showEntries_) {
			POI currPOI = mid_.poiRepo_.showEntryParent_;
			if (currPOI == null) {
				DisplayPoi dp = mid_.getCurrentPoiSelection(false, detailView_).getCurrentPOI();
				if (dp != null && dp.poi_ != null) {
					currPOI = dp.poi_;
				} else {
					return false;
				}
			}
			mid_.poiRepo_.entryUrl_ = ConfigManager.SERVER_URI + currPOI.showEntryUrl_;
			mid_.poiRepo_.lastSelected_ = mid_.poiListSelection_.bv_.currentItem_;
			mid_.channelEntity_.loadContentEntry(currPOI, detailView_);
		} else if (component instanceof Label) {
			Label label = (Label) component;
			if (label.isHyperLink_) {
				detailView_.dealWithLink(label.linkAddr_, detailView_, false);
			} else if (detailView_.isShowQueryView_) {
				detailView_.keyActionSoftKey();
			} else {
				return false;
			}
		} else if ((component instanceof ImageItem) && ((ImageItem) component).isHyperLink_) {
			ImageItem imgItem = (ImageItem) component;
			detailView_.dealWithLink(imgItem.linkAddr_, detailView_, false);
		} else if (component instanceof Table) {
			return GeneralController.disposeTableEvent(component, detailView_);
		} else if (component instanceof Select) {
			Select selct = (Select) component;
			Utils.printOutToConsole("actionUrl_ = " + selct.actionUrl_);
			if (selct.actionUrl_ == null) {
				return false;
			}
			StringBuffer linkB = new StringBuffer(selct.actionUrl_);
			linkB.append("&");
			linkB.append(selct.attrName_);
			linkB.append("=");
			if(selct.optionValue_.indexOf('+') != -1) {
				int index = selct.optionValue_.indexOf('+');
				linkB.append(selct.optionValue_.substring(0, index));
				linkB.append('%');
				linkB.append("2B");
				linkB.append(selct.optionValue_.substring(index + 1));
			} else {
				linkB.append(selct.optionValue_);
			}
			
			Utils.printOutToConsole("link = " + linkB.toString());
			detailView_.dealWithLink(linkB.toString(), detailView_, false);
		}

		return isAction;
	}

	private void componentActionPOIListView(View component) {
		// TODO Auto-generated method stub
		if (component == detailView_.prevItem_) {
			detailView_.changePage(false);
			if (!detailView_.isShowEntry_) {
				mid_.channelEntity_.updateBelowPOIList();
			} else {
				mid_.channelEntity_.loadContentEntry(mid_.poiRepo_.showEntryParent_, detailView_);
			}
		} else if (component == detailView_.nextItem_) {
			detailView_.changePage(true);
			if (!detailView_.isShowEntry_) {
				mid_.channelEntity_.updateBelowPOIList();
			} else {
				mid_.channelEntity_.loadContentEntry(mid_.poiRepo_.showEntryParent_, detailView_);
			}
		} else {
			PoiSelection poiSel = mid_.getCurrentPoiSelection(false, detailView_);
			int firstDisplayIndex = poiSel.getPoiIndex(0);
			poiSel.setCurrentDisplayIndex(detailView_.currentItem_ - firstDisplayIndex);
			detailView_.detailType_ = DetailView.POI_DETAIL;
			detailView_.setShowDetail(null);
			Utils.addManagerAndSetTop(detailView_);
		}
	}

	private void componentActionChannelListView(View component) {
		if (component == detailView_.prevItem_) {
			detailView_.changePage(false);
			mid_.channelEntity_.updateBelowChannelList();
		} else if (component == detailView_.nextItem_) {
			detailView_.changePage(true);
			mid_.channelEntity_.updateBelowChannelList();
		} else {
			mid_.belowChannelRepo_.setCurrentChannelID(detailView_.currentItem_);
			mid_.channelEntity_.stepDownDetailView(detailView_, ((mButton)component).isTopChannelAlone_);
		}
	}

}

